#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_proc_launcher.sh
##  Functions : Run procedure
##  Purpose   : Run procedure
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_proc_launcher.sh 5Q01 0'
    echo -e '\n${V_SHELL_HOME}/edw_proc_launcher.sh ALL_ORG 1'
}

function START(){
    V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET START_TIME = NOW() WHERE DATA_DATE = '${1}' AND ORG_TYPE = '${2}' AND JOB_GROUP = '${3}' AND PROC_NAME = '${4}';"
 ${V_RUN_MYSQL} -q "${V_QUERY}"
}

function END(){
    V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET END_TIME = NOW() WHERE DATA_DATE = '${1}' AND ORG_TYPE = '${2}' AND JOB_GROUP = '${3}' AND PROC_NAME = '${4}';"
    ${V_RUN_MYSQL} -q "${V_QUERY}"
}

function RUN(){
    V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET STATUS = 'RUN' WHERE DATA_DATE = '${1}' AND ORG_TYPE = '${2}' AND JOB_GROUP = '${3}' AND PROC_NAME = '${4}';"
    ${V_RUN_MYSQL} -q "${V_QUERY}"
}

function SKIP(){
    V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET STATUS = 'SKIP' WHERE DATA_DATE = '${1}' AND ORG_TYPE = '${2}';"
    ${V_RUN_MYSQL} -q "${V_QUERY}"
}

function DONE(){
    V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET STATUS = 'DONE' WHERE DATA_DATE = '${1}' AND ORG_TYPE = '${2}' AND JOB_GROUP = '${3}' AND PROC_NAME = '${4}';"
    ${V_RUN_MYSQL} -q "${V_QUERY}"
    if [[ $? -ne 0 ]];then
        echo -e "\nERROR:Can not update procedure status."
        echo -e "${V_QUERY}"
        exit 1
    fi
}

function FAIL(){
    V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET STATUS = 'FAIL' WHERE DATA_DATE = '${1}' AND ORG_TYPE = '${2}' AND JOB_GROUP = '${3}' AND PROC_NAME = '${4}';"
    ${V_RUN_MYSQL} -q "${V_QUERY}"
    if [[ $? -ne 0 ]];then
        echo -e "\nERROR:Can not update procedure status."
        echo -e "${V_QUERY}"
        exit 1
    fi
}

# get the parameter
V_DATA_SRC_ORG=${1}
V_PARALLEL_CNT=${2}

# ensure the basic variavle of connection is not null
if [[ -z ${V_DATA_SRC_ORG} || -z ${V_PARALLEL_CNT} ]];then
    USAGE
    exit 1
fi

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_SCRIPT_NAME}_${V_DATA_SRC_ORG}_${V_PARALLEL_CNT}_${V_TIME_STAMP}_$$.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output
echo -e "[Begin: $(date +%F) $(date +%T)]"

echo -e "\n[------------------------Thread:${V_DATA_SRC_ORG}:${V_PARALLEL_CNT}------------------------]"
# Check the parm file if not exists
while true;do
    if [[ -f "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt" ]];then
        break
    fi
    echo -e "WARNING:Waiting for parameter file after $((${V_WAIT_TIME}/60)) mins..."
    sleep ${V_WAIT_TIME}
done

V_PARM_FILE="${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo -e "\nSuccess to get parm file:${V_PARM_FILE}"

# Get data date
V_DATA_DATE=$(grep V_DATA_DATE ${V_PARM_FILE} | awk -F'=' '{print $2}')
if [[ -z ${V_DATA_DATE} ]];then
    echo -e "\nERROR:Can not get DATA_DATE."
    exit 1
else
    echo -e "\nData date is:${V_DATA_DATE}."
fi

# Check the batch if need to run
V_BATCH_IND=$(grep BATCH_IND ${V_PARM_FILE} | awk -F'=' '{print $2}')
if [[ ${V_BATCH_IND} != 'Y' ]];then
    SKIP ${V_DATA_DATE} ${V_DATA_SRC_ORG}
    echo -e "\nSkip the batch!"
    echo -e "\n[End: $(date +%F) $(date +%T)]"
    exit 0
else
    echo -e "\nReady to run..."
fi

# For rerun and initinal status is null
V_QUERY="UPDATE ETL.EDW_JOB_STATUS SET STATUS = NULL WHERE DATA_DATE = '${V_DATA_DATE}' AND ORG_TYPE = '${V_DATA_SRC_ORG}' AND MOD(SEQ_NO,${V_PARALLEL_NO}) = '${V_PARALLEL_CNT}' AND STATUS NOT IN('DONE','SKIP');"
${V_RUN_MYSQL} -q "${V_QUERY}"
if [[ $? -eq 0 ]];then
    echo -e "\nInitinal the table[EDW_JOB_STATUS] status successfully."
else
    echo -e "\nERROR:Can not refresh the status."
    echo -e "${V_QUERY}"
    exit 1
fi

# Get the group list
V_QUERY="SELECT DISTINCT JOB_GROUP FROM ETL.EDW_JOB_STATUS WHERE DATA_DATE = '${V_DATA_DATE}' AND ORG_TYPE = '${V_DATA_SRC_ORG}' ORDER BY JOB_GROUP;"
V_GROUP_LIST=$(${V_RUN_MYSQL} -q "${V_QUERY}")
for GROUP in ${V_GROUP_LIST};do
    echo -e "\n[-----------Start to group:${GROUP}:${V_PARALLEL_CNT}-----------]"
    V_QUERY="SELECT DISTINCT PROC_NAME FROM ETL.EDW_JOB_STATUS WHERE DATA_DATE = '${V_DATA_DATE}' AND ORG_TYPE = '${V_DATA_SRC_ORG}' AND JOB_GROUP = '${GROUP}' AND MOD(SEQ_NO,${V_PARALLEL_NO}) = '${V_PARALLEL_CNT}' AND STATUS IS NULL ORDER BY SEQ_NO;"
    V_PROC_LIST=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    for PROC in ${V_PROC_LIST};do
        START ${V_DATA_DATE} ${V_DATA_SRC_ORG} ${GROUP} ${PROC}
        # Check the proc file if not exists
        if [[ ! -f "${V_SHELL_PROC}/${PROC}" ]];then
            FAIL ${V_DATA_DATE} ${V_DATA_SRC_ORG} ${GROUP} ${PROC}
            echo -e "ERROR:Can not find the procedure file:${V_SHELL_PROC}/${PROC}"
            continue
        else
            V_PROC_FILE="${V_SHELL_PROC}/${PROC}"
            echo -e "\n***RUN procedure:${V_PROC_FILE}***"
        fi
        
        # Run procedure
        RUN ${V_DATA_DATE} ${V_DATA_SRC_ORG} ${GROUP} ${PROC}
        echo -e "Call:${V_RUN_HIVQL} -o ${V_DATA_SRC_ORG} -f ${PROC}"
        ${V_RUN_HIVQL} -o ${V_DATA_SRC_ORG} -f ${PROC}
        if [[ $? -eq 0 ]];then
            DONE ${V_DATA_DATE} ${V_DATA_SRC_ORG} ${GROUP} ${PROC}
            echo -e "Success to run procedure:${V_PROC_FILE}"
        else
            FAIL ${V_DATA_DATE} ${V_DATA_SRC_ORG} ${GROUP} ${PROC}
            echo -e "ERROR:Fail to run procedure[${PROC}]."
        fi
        
        END ${V_DATA_DATE} ${V_DATA_SRC_ORG} ${GROUP} ${PROC}
    done
    
    # Waiting for all jobs completed
    while true;do
        V_QUERY="SELECT COUNT(*) FROM ETL.EDW_JOB_STATUS WHERE DATA_DATE = '${V_DATA_DATE}' AND ORG_TYPE = '${V_DATA_SRC_ORG}' AND JOB_GROUP = '${GROUP}' AND (STATUS IS NULL OR STATUS = 'RUN');"
        V_DONE_CNT=$(${V_RUN_MYSQL} -q "${V_QUERY}")
        if [[ ${V_DONE_CNT} -eq 0 ]];then
            break
        else
            echo -e "\nINFO:Waiting for other jobs in same group..."
            sleep ${V_WAIT_TIME}
        fi
    done
    
    # Check each group
    V_QUERY="SELECT COUNT(*) FROM ETL.EDW_JOB_STATUS WHERE DATA_DATE = '${V_DATA_DATE}' AND ORG_TYPE = '${V_DATA_SRC_ORG}' AND JOB_GROUP = '${GROUP}' AND STATUS = 'FAIL';"
    V_FAIL_CNT=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    if [[ ${V_FAIL_CNT} -ne 0 ]];then
        echo -e "\nERROR:Group ${GROUP} has fail procedures, pls Check table EDW_JOB_STATUS."
        exit ${V_FAIL_CNT}
    fi
done

echo -e "\n[End: $(date +%F) $(date +%T)]"
exit 0
